<template>
	<view class="page_wrapper">
		<image class="page_bg" src="/static/img/bg.png" mode=""></image>
		<image class="login_icon" src="/static/img/login_icon.png" mode=""></image>
		<view class="login_wrapper">
			<view class="login_title">{{(loginType=='pwd'?'账号密码登陆':'验证码登陆')}}</view>
			<view class="login_row flexRow">
				<image src="/static/img/phone.png" class="row_icon" mode=""></image>

				<view class="row_content">
					<input placeholder-class="plc_input" v-model="phone" value="" placeholder="请输入账号" maxlength="16" />
				</view>
			</view>
			<view class="login_row flexRow" v-show="loginType=='pwd'">
				<image src="/static/img/lock.png" class="row_icon" mode=""></image>
				<view class="row_content">
					<input placeholder-class="plc_input" :type="inputType" value="" v-model="pwd" placeholder="请输入账号密码" maxlength="18" />
					<view class="show_btn_box" @tap="setInputType">
						<image class="show_btn" src="/static/img/showPwd.png" mode=""></image>
					</view>
				</view>
			</view>
			<view class="login_row flexRow" v-show="loginType=='code'">
				<image src="/static/img/lock.png" class="row_icon" mode=""></image>

				<view class="row_content">
					<input placeholder-class="plc_input" type="number" value="" placeholder="请输入验证码" maxlength="8" @input="setCode" />
					<view class="send_btn" :class="{disabel_send: disabelSend}" @tap="send_code">
						<text v-if="!disabelSend">获取短信</text> <text v-if="disabelSend">重新获取({{dCount}}s)</text>
					</view>
				</view>
			</view>
			<view class="toggle_login flexRow">
				<!-- <view class="" @tap="toggleLogin">
					{{(loginType=='pwd'?'切换验证码登陆':'切换密码登陆')}}
				</view> -->
				<navigator class="register" url="/pages/register/register?type=0">账号注册</navigator>
				<navigator class="forget_pwd" url="/pages/register/register?type=1">忘记密码</navigator>
			</view>
			<view class="login_btn" @tap="phoneLogin">
				登录
			</view>
		</view>
	</view>
</template>

<script>
	import request from "../../apiRequest/request.js"
	export default {
		data() {
			return {
				phone: '',
				code: '',
				pwd: '',
				inputType: 'password',
				loginType: 'pwd',
				timer: '',
				dCount: 60,
				disabelSend: false
			}
		},
		methods: {
			toggleLogin() {
				if (this.loginType == 'pwd') {
					this.loginType = 'code'
				} else {
					this.loginType = 'pwd'
				}
			},
			setInputType() {
				if (this.inputType == 'password') {
					this.inputType = 'text'
				} else {
					this.inputType = 'password'
				}
			},
			setPhone(e) {
				this.phone = e.detail.value
			},
			setCode(e) {
				this.code = e.detail.value
			},
			testPhone() {
				let phone = this.phone;
				return phone.replace(/[^\w\.\/]/ig, '')
				// if(!this.phone) return false
				// return /^1[3456789]\d{9}$/.test(this.phone)
			},
			getPwd() {
				let pwd = this.pwd;
				return pwd.replace(/[^\w\.\/]/ig, '')
			},
			initTimer() {
				this.disabelSend = true
				this.dCount = 60
				this.timer = setInterval(() => {
					if (this.dCount == 0) {
						this.disabelSend = false
						clearInterval(this.timer)
					} else {
						this.dCount--
					}
				}, 1000)
			},
			send_code() {
				if (/^1[3456789]\d{9}$/.test(this.phone)) {
					if(this.disabelSend){
						return
					}
					request.sendCode(this.phone).then(res => {
						this.initTimer()
						console.log(res)
						uni.showToast({
							title: '短信发送成功',
							duration: 2000
						})
					}).catch(err => {
						this.$unexpected(err)
					})
				} else {
					uni.showToast({
						title: '请确认手机格式',
						icon: 'none',
						duration: 2000
					})
				}
			},
			phoneLogin() {
				let phone = '',
					pwd = '',
					code = '';
				if (/^1[3456789]\d{9}$/.test(this.phone)) {
					phone = this.phone
				}else{
					uni.showToast({
						title: '请确认手机格式',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (!this.getPwd()) {
					uni.showToast({
						title: '密码不能为空或带有特殊字符',
						icon: 'none',
						duration: 2000
					})
					return
				}
				pwd = this.getPwd()	
				// if (this.loginType == 'pwd') {
					
				// } 
				// else {
				// 	if (/^1[3456789]\d{9}$/.test(this.phone)) {
				// 		phone = this.phone
				// 	} else {
				// 		uni.showToast({
				// 			title: '请确认手机格式',
				// 			icon: 'none',
				// 			duration: 2000
				// 		})
				// 		return
				// 	}
				// 	code = this.code
				// }
				console.log(phone, pwd)
				request.apiLogin(phone, pwd).then(res => {
					uni.showToast({
						title: '登录成功',
						icon: 'none',
						duration: 2000
					})
					uni.setStorageSync('token', res.data.token)
					uni.reLaunch({
						url: '/pages/personal/personal'
					})
				}).catch(err => {
					this.$unexpected(err)
				})

			}
		}
	}
</script>

<style>
	.page_wrapper {
		width: 100%;
		height: 100vh;
		position: relative;
	}

	.toggle_login {
		padding: 20rpx 136rpx;
		font-size: 28rpx;
		justify-content: space-between;
	}

/* 	.toggle_login view {
		padding: 14rpx 20rpx;
		border: 1rpx solid rgba(238, 238, 238, 1);
		border-radius: 10rpx;
		background-color: #0489FF;
		color: #FFFFFF;
	} */

	.page_bg {
		width: 100%;
		height: 100%;
		position: absolute;
		top: 0;
		left: 0;
		z-index: -1;
	}

	.login_wrapper {
		width: 100%;
		position: absolute;
		top: 509rpx;
		/* transform:translateY(-42%); */
	}

	.login_title {
		font-size: 34rpx;
		font-family: Source Han Sans SC;
		font-weight: 500;
		color: rgba(4, 137, 255, 1);
		text-align: center;
		margin-bottom: 20rpx;
	}

	.login_icon {
		display: block;
		width: 241rpx;
		height: 232rpx;
		position: absolute;
		top: 260rpx;
		left: 50%;
		transform: translateX(-50%);
	}

	.row_icon {
		width: 36rpx;
		height: 54rpx;
		margin-right: 20rpx;
	}

	.login_row {
		padding: 20rpx 136rpx;
		align-items: center;

	}

	.row_content {
		flex: 1;
		position: relative;
	}

	.row_content>input {
		padding: 10rpx 5rpx;
		font-size: 32rpx;
		font-family: Source Han Sans SC;
		font-weight: 400;
		border-bottom: 2rpx solid rgba(238, 238, 238, 1);
	}

	.plc_input {
		color: rgba(186, 186, 186, 1);
	}

	.send_btn {
		position: absolute;
		right: 0;
		bottom: 10rpx;
		/* width: 180rpx; */
		padding: 0 16rpx;
		height: 60rpx;
		line-height: 60rpx;
		text-align: center;
		background-color: #0489FF;
		color: #FFFFFF;
		font-size: 26rpx;
		border-radius: 10rpx;
		z-index: 100;
	}

	.login_btn {
		width: 400rpx;
		height: 80rpx;
		line-height: 80rpx;
		background: linear-gradient(0deg, rgba(4, 137, 255, 1), rgba(145, 202, 255, 1));
		border-radius: 10rpx;
		font-size: 32rpx;
		font-family: Source Han Sans SC;
		font-weight: 500;
		color: rgba(255, 255, 255, 1);
		margin: 0 auto;
		margin-top: 120rpx;
		text-align: center;
	}

	.show_btn_box {
		position: absolute;
		height: 60rpx;
		width: 60rpx;
		right: 0;
		text-align: center;
		bottom: 0;
		z-index: 100;
	}

	.show_btn {
		width: 30rpx;
		height: 18rpx;
	}
	.disabel_send{
		background-color: rgba(186, 186, 186, 1);
	}
</style>
